package com.ruoyi.comprehensive.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.pengjie.SnowflakeIdUtils;
import com.ruoyi.comprehensive.domain.Prevention;
import com.ruoyi.comprehensive.domain.PreventionEnclosure;
import com.ruoyi.comprehensive.dto.PreventionExamineDto;
import com.ruoyi.comprehensive.mapper.PreventionEnclosureMapper;
import com.ruoyi.comprehensive.mapper.PreventionMapper;
import com.ruoyi.comprehensive.vo.PreventionExamineVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.comprehensive.mapper.PreventionExamineMapper;
import com.ruoyi.comprehensive.domain.PreventionExamine;
import com.ruoyi.comprehensive.service.IPreventionExamineService;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 通防审核Service业务层处理
 * 
 * @author hcc
 * @date 2023-12-16
 */
@Service
public class PreventionExamineServiceImpl implements IPreventionExamineService 
{
    @Autowired
    private PreventionExamineMapper preventionExamineMapper;
    @Autowired
    private PreventionMapper preventionMapper;
    @Autowired
    private PreventionEnclosureMapper preventionEnclosureMapper;

    /**
     * 查询通防审核
     * 
     * @param id 通防审核主键
     * @return 通防审核
     */
    @Override
    public PreventionExamine selectPreventionExamineById(String id)
    {
        return preventionExamineMapper.selectPreventionExamineById(id);
    }

    /**
     * 查询通防审核列表
     * 
     * @param preventionExamine 通防审核
     * @return 通防审核
     */
    @Override
    public List<PreventionExamine> selectPreventionExamineList(PreventionExamine preventionExamine)
    {
        return preventionExamineMapper.selectPreventionExamineList(preventionExamine);
    }

    /**
     * 新增通防审核
     * 
     * @param preventionExamine 通防审核
     * @return 结果
     */
    @Override
    public int insertPreventionExamine(PreventionExamine preventionExamine)
    {
        preventionExamine.setId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
        return preventionExamineMapper.insertPreventionExamine(preventionExamine);
    }

    /**
     * 修改通防审核
     * 
     * @param preventionExamine 通防审核
     * @return 结果
     */
    @Override
    public int updatePreventionExamine(PreventionExamine preventionExamine)
    {
        PreventionExamine preventionExamine1 = new PreventionExamine();
        BeanUtils.copyProperties(preventionExamine,preventionExamine1);
        preventionExamine1.setUpdateAt(new Date());
        preventionExamine1.setUpdateByName(SecurityUtils.getUsername());
        preventionExamine1.setUpdateBy(SecurityUtils.getUserId().toString());
        Prevention prevention = preventionMapper.selectPreventionById(preventionExamine.getPreventionId());
        if (prevention != null){
            Prevention prevention1 = new Prevention();
            prevention1.setUpdateAt(new Date());
            prevention1.setUpdateByName(SecurityUtils.getUsername());
            prevention1.setUpdateBy(SecurityUtils.getUserId().toString());
            prevention1.setExamineStatus(preventionExamine.getExamineStatus());
            preventionMapper.updatePrevention(prevention1);
        }
        return preventionExamineMapper.updatePreventionExamine(preventionExamine1);
    }

    /**
     * 批量删除通防审核
     * 
     * @param ids 需要删除的通防审核主键
     * @return 结果
     */
    @Override
    public int deletePreventionExamineByIds(String[] ids)
    {
        return preventionExamineMapper.deletePreventionExamineByIds(ids);
    }

    /**
     * 删除通防审核信息
     * 
     * @param id 通防审核主键
     * @return 结果
     */
    @Override
    public int deletePreventionExamineById(String id)
    {
        return preventionExamineMapper.deletePreventionExamineById(id);
    }

    /**
     * 获取通防审核列表
     * @param vo
     * @return
     */
    @Override
    public PageInfo<PreventionExamineDto> getPreventionExamineList(PreventionExamineVo vo) {
        // 开始分页
        startPage();
        List<PreventionExamine> preventionExamineList = preventionExamineMapper.getPreventionExamineList(vo);
        //  获取分页信息
        PageInfo preventionExamineDtoInfo = new PageInfo<>(preventionExamineList);
        List<PreventionExamineDto> list = preventionExamineList.stream().map(preventionExamine -> {
            PreventionExamineDto preventionExamineDto = new PreventionExamineDto();
            BeanUtils.copyProperties(preventionExamine, preventionExamineDto);
            Prevention prevention = preventionMapper.selectPreventionById(preventionExamine.getPreventionId());
            preventionExamineDto.setName(prevention.getName());
            preventionExamineDto.setPreventionDate(prevention.getPreventionDate());
            preventionExamineDto.setPreventionCode(prevention.getPreventionCode());
            return preventionExamineDto;
        }).collect(Collectors.toList());
        preventionExamineDtoInfo.setList(list);
        return preventionExamineDtoInfo;
    }

    /**
     * 获取通防审核详细信息
     * @param id
     * @return
     */
    @Override
    public PreventionExamineDto getPreventionExamineById(String id) {
        PreventionExamineDto preventionExamineDto = new PreventionExamineDto();
        PreventionExamine preventionExamine = preventionExamineMapper.selectPreventionExamineById(id);
        BeanUtils.copyProperties(preventionExamine,preventionExamineDto);
        Prevention prevention = preventionMapper.selectPreventionById(preventionExamine.getPreventionId());
        PreventionEnclosure preventionEnclosure = new PreventionEnclosure();
        preventionEnclosure.setPreventionId(preventionExamine.getId());
        preventionEnclosure.setEnclosureType("5");
        List<PreventionEnclosure> preventionEnclosures = preventionEnclosureMapper.selectPreventionEnclosureList(preventionEnclosure);
        preventionExamineDto.setEnclosures(preventionEnclosures);
        preventionExamineDto.setPreventionCode(prevention.getPreventionCode());
        preventionExamineDto.setPreventionDate(prevention.getPreventionDate());
        preventionExamineDto.setName(prevention.getName());
        return preventionExamineDto;
    }
}
